package cn.coastalinfo.project.drools.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.coastalinfo.common.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.coastalinfo.common.utils.StringUtils;
    import cn.coastalinfo.framework.web.page.PageQuery;
    import cn.coastalinfo.framework.web.page.TableDataInfo;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.coastalinfo.project.drools.domain.bo.RuleVariableBo;
import cn.coastalinfo.project.drools.domain.vo.RuleVariableVo;
import cn.coastalinfo.project.drools.domain.RuleVariable;
import cn.coastalinfo.project.drools.mapper.RuleVariableMapper;
import cn.coastalinfo.project.drools.service.IRuleVariableService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 参数Service业务层处理
 *
 * @author hadge
 * @date 2024-06-07
 */
@RequiredArgsConstructor
@Service
public class RuleVariableServiceImpl extends ServiceImpl<RuleVariableMapper, RuleVariable> implements IRuleVariableService {

    @Autowired
    private RuleVariableMapper baseMapper;

    /**
     * 查询参数
     */
    @Override
    public RuleVariableVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

        /**
         * 查询参数列表
         */
        @Override
        public TableDataInfo<RuleVariableVo> queryPageList(RuleVariableBo bo, PageQuery pageQuery) {
            LambdaQueryWrapper<RuleVariable> lqw = buildQueryWrapper(bo);
            Page<RuleVariableVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
            return TableDataInfo.build(result);
        }

    /**
     * 查询参数列表
     */
    @Override
    public List<RuleVariableVo> queryList(RuleVariableBo bo) {
        LambdaQueryWrapper<RuleVariable> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RuleVariable> buildQueryWrapper(RuleVariableBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RuleVariable> lqw = Wrappers.lambdaQuery();
                    lqw.like(StringUtils.isNotBlank(bo.getLabel()), RuleVariable::getLabel, bo.getLabel());
                    lqw.eq(StringUtils.isNotBlank(bo.getDescription()), RuleVariable::getDescription, bo.getDescription());
//                    lqw.eq(StringUtils.isNotBlank(bo.getValueType()), RuleVariable::getValueType, bo.getValueType());

                    lqw.eq(bo.getValueType() != null, RuleVariable::getValueType, bo.getValueType());
                    lqw.like(StringUtils.isNotBlank(bo.getGroupName()), RuleVariable::getGroupName, bo.getGroupName());
                    lqw.eq(StringUtils.isNotBlank(bo.getValue()), RuleVariable::getValue, bo.getValue());
//                    lqw.eq(bo.getDeleted() != null, RuleVariable::getDeleted, bo.getDeleted());
        return lqw;
    }

    /**
     * 新增参数
     */
    @Override
    public Long insertByBo(RuleVariableBo bo) {
        RuleVariable add = BeanUtil.toBean(bo, RuleVariable.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return add.getId();
    }

    /**
     * 修改参数
     */
    @Override
    public Long updateByBo(RuleVariableBo bo) {
        RuleVariable update = BeanUtil.toBean(bo, RuleVariable.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        return update.getId();
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RuleVariable entity){
        long id = ObjectUtil.isNull(entity.getId()) ? -1L : entity.getId();
        LambdaQueryWrapper<RuleVariable> lqw = Wrappers.lambdaQuery();
        RuleVariable info = baseMapper.selectOne(new LambdaQueryWrapper<RuleVariable>()
                .select(RuleVariable::getId, RuleVariable::getLabel,RuleVariable::getValue)
                .eq(RuleVariable::getLabel, entity.getLabel())
                .or()
                .eq(RuleVariable::getValue, entity.getValue()));
        if (ObjectUtil.isNotNull(info) && (id < 0 || info.getId() != id)) {
            if(info.getLabel().equals(entity.getLabel()) ){
                throw new ServiceException("参数中文名称不能重复");
            }
            throw new ServiceException("参数编码不能重复");
        }
    }

    /**
     * 批量删除参数
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
